﻿using DataService.Core.ServiceDTO;
using DataService.Main;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace UnitTestCore3._1
{
    internal class Program
    {
        private static string  _Connstr = string.Empty;
        private static ConcurrentDictionary<int, List<string>> _dbTables = new ConcurrentDictionary<int, List<string>>();
        static void Main(string[] args)
        {
             CreateTableBase();
            Console.WriteLine("完成");
        }
        public static T dddd<T>(T t)
        {
            return default(T);
        }

        public static void CreateTableBase()
        {
 

            using (var sqlitedb = DataService.DBMangerFactory.CreateProvider(DatabaseType.SQLite))
            {
                string path = DAL.GetModelPath();
                string connectionString = string.Format("Data Source={0};", path);
                sqlitedb.ConnectionString = connectionString;

                var tableNames = ModelDAL.FindAllModeTableNames(sqlitedb);

                var modelInitDatas = DAL.Query<ModelInitData>(x => true, sqlitedb).ToList();

                string tempDataBaseFilePath = @"D:\DataBaseBack";
                string dataBaseName = "Test_CCC";

                using (var sqldb = DataService.DBMangerFactory.CreateProvider(DatabaseType.SqlServer))
                {
                    sqldb.ConnectionString = _Connstr;

                    ModelDAL.CreateDataBaseSchema(tempDataBaseFilePath, dataBaseName, sqldb);
                }
                List<string> newConns = new List<string>();
               var tempArray= _Connstr.Split(';');
                foreach (var item in tempArray)
                {
                    if (item.Split('=')[0].ToLower() == "database")
                    {
                        newConns.Add($"Database={dataBaseName}");
                    }
                    else
                    {
                        newConns.Add(item);
                    }
                }
                string newConnStr=string.Join(";", newConns);
                using (var sqldb = DataService.DBMangerFactory.CreateProvider(DatabaseType.SqlServer))
                {
                    sqldb.ConnectionString = newConnStr;
                    Console.WriteLine(sqldb.ConnectionString);
                    foreach (var itemTableName in tableNames)
                    {
                        ModelDAL.CreatTableSchema(itemTableName, sqldb);
                    }
                    ModelDAL.FullDBInitData(modelInitDatas, sqldb);

                }

            }





        }
    }
}
